﻿using System;

using OctoOS.UI;
using OctoOS;

public partial class __myArticles : OOSPage
{
	class PathInfo
	{
		[PathInfoMember(0)]
		[RangeValidator(1, 3)]
		internal byte Type;

		[PathInfoMember(0, true)]
		internal int PageIndex = 0;

		public MyArticleTypes ArticleTypes;

		void PostInit()
		{
			if(PageIndex > 0)
				PageIndex--;

			ArticleTypes = (MyArticleTypes)Type;
		}
	}

	int _artOnPage = 20;
	PathInfo _pathInfo;

	protected void Page_Init(object sender, EventArgs e)
	{
		OOSRequest.Current.EnsureSignIn();
		_pathInfo = EnablePathInfo<PathInfo>();
	}

    protected void Page_Load(object sender, EventArgs e)
    {
		PageTitle = Resources.Main.myArticles_Title;
		Commands.AddCommand(Resources.Main.shared_AddArticle, Urls.GetAddArticle());
		AddBreadcrumb(string.Empty, "/");

		int totalCount;
		int startIndex = _pathInfo.PageIndex * _artOnPage;
		var allCategories = AppUtil.GetCategories(OOSRequest.Current);

		var myArticles = ApiService.GetMyArticles(OOSRequest.Current, _pathInfo.ArticleTypes,
			startIndex, _artOnPage, out totalCount, allCategories);

		if(totalCount == 0)
		{
			empty.Visible = true;
			empty.SetContent(Resources.Main.myArticles_EmptyMessage);
		}
		else
		{
			myPager.SelectedIndex = _pathInfo.PageIndex;
			myPager.FirstPageUrl = Urls.GetMyArticles();
			myPager.PageUrlPattern = Urls.GetMyArticles() + "/{0}";
			myPager.PageCount = NumericPager.CalculatePageCount(totalCount, _artOnPage);

			var categories = AppUtil.GetCategories(OOSRequest.Current);

			foreach(var article in myArticles)
				articlesList.Items.AddItem(new MyArticleUIPreview(article, categories));
		}
    }
}